package microBlogger.screens;

import context.*;


import java.io.IOException;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.Ticker;

import microBlogger.MicroBlogger;

import bloggerAPI.BlogEntry;
import bloggerAPI.j2me.BloggerConnection;

// ******************************************************************
// *** CLASS: EntryScreen
// ******************************************************************
public class EntryScreen extends Form implements Debug.Constants
{ //private static final boolean DEBUG=true; // Overriding Debug.Constants.DEBUG
  private static final Command sendCommand=new Command("Send",Command.OK,0);
  private static final Command backCommand=new Command("Back",Command.EXIT,1);
  private Display display;
  private Displayable prev;
  private MicroBlogger midlet;
  private BloggerConnection bloggerConnection;
  private BlogEntry blogEntry;
  private boolean isEdit;
  private TextField titleField;
  private TextField contentField;
  
  public EntryScreen(MicroBlogger midlet,BloggerConnection bloggerConnection,BlogEntry blogEntry,boolean isEdit)
  { super(isEdit?"Edit Entry:":"New Entry:");
    this.display=Display.getDisplay(midlet);
    this.midlet=midlet;
    this.bloggerConnection=bloggerConnection;
    this.blogEntry=blogEntry;
    this.isEdit=isEdit;
    String title=null;
    String content=null;
  	
    if(isEdit)
    { title=(String)blogEntry.getTitle();
      content=blogEntry.getText();
    }
    titleField=new TextField("Title",title,(title==null)?400:title.length()+200,TextField.ANY);
    contentField=new TextField("Content",content,(content==null)?400:content.length()+200,TextField.ANY);
    append(titleField);
    append(contentField);
    this.addCommand(sendCommand);
    this.addCommand(backCommand);
    this.setCommandListener(commandListener);
  }
  
  private CommandListener commandListener=new CommandListener()
  { public void commandAction(Command c,Displayable s)
    { if(c==backCommand) display.setCurrent(prev);
      else if(c==sendCommand) send();
    }
  };
  
  public void show()
  { this.prev=display.getCurrent();
	display.setCurrent(this);
  }
  
  private void send()
  { new Thread("Sending")
	{ public void run()
	  { try
	    { String title=titleField.getString();
		  setTicker(new Ticker("... Sending "+title+" ..."));
		  blogEntry.setTitle(title);
	      blogEntry.setText(contentField.getString());
    	  if(isEdit) bloggerConnection.editBlogEntry(blogEntry);
          else bloggerConnection.postBlogEntry(blogEntry);
    	  setTicker(null);
   	      midlet.getEntryListScreen().show();
	  	}
	    catch(IOException e) { Debug.EXCEPTION("MicroBlogger.connect",e); }
	  }
	}.start();
  }
  
}
